package cn.edu.zut.dao;

import cn.edu.zut.domain.ContestRegistry;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ContestRegistryDao {

    @Select("select cr.*, u.userName, c.contestName, dep.name depName, u.department_id from contestregistry cr, user u, contest c, dep where cr.STNumberId = u._id and cr.cid = c.contestId and u.department_id = dep._id order by cr._id desc;")
    @Results(id = "contestRegistryMap", value = {
            @Result(id = true, column = "_id", property = "_id"),
            @Result(column = "cid", property = "contest.contestId"),
            @Result(column = "STNumberId", property = "user._id"),
            @Result(column = "ClassAndGrade", property = "ClassAndGrade"),
            @Result(column = "email", property = "email"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "relName", property = "relName"),
            @Result(column = "userName", property = "user.userName"),
            @Result(column = "contestName", property = "contest.contestName"),
            @Result(column = "depName", property = "user.dep.name"),
            @Result(column = "department_id", property = "user.dep._id")
    })
    public List<ContestRegistry> findAll();

    @Delete("delete from contestregistry where _id = #{_id};")
    public void deleteById(int id);

    @Update("update contestregistry set relName = #{relName}, ClassAndGrade = #{ClassAndGrade}, email = #{email} where _id = #{_id};")
    public void update(ContestRegistry contestRegistry);


    @Select("<script>" +
            "select cr.*, u.userName, c.contestName, dep.name depName, u.department_id from contestregistry cr, user u, contest c, dep where cr.STNumberId = u._id and cr.cid = c.contestId and u.department_id = dep._id" +
            "<if test=\"user != null and user.userName != null and user.userName != ''\">" +
            " and u.userName like #{user.userName}" +
            "</if>" +
            "<if test=\"contest != null and contest.contestName != null and contest.contestName != ''\">" +
            " and c.contestName like #{contest.contestName}" +
            "</if>" +
            "<if test=\"user != null and user.dep != null and user.dep.name != null and user.dep.name != ''\">" +
            " and dep.name like #{user.dep.name}" +
            "</if>" +
            "<if test=\"ClassAndGrade != null and ClassAndGrade != ''\">" +
            " and ClassAndGrade like #{ClassAndGrade}" +
            "</if>" +
            " order by cr._id desc"
            + "</script>"
    )
    //@RequestMapping(value = "contestRegistryMap")
    @Results(value = {
            @Result(id = true, column = "_id", property = "_id"),
            @Result(column = "cid", property = "contest.contestId"),
            @Result(column = "STNumberId", property = "user._id"),
            @Result(column = "ClassAndGrade", property = "ClassAndGrade"),
            @Result(column = "email", property = "email"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "relName", property = "relName"),
            @Result(column = "userName", property = "user.userName"),
            @Result(column = "contestName", property = "contest.contestName"),
            @Result(column = "depName", property = "user.dep.name"),
            @Result(column = "department_id", property = "user.dep._id")
    })
    public List<ContestRegistry> findLikeAllByPrint(ContestRegistry contestRegistry);

    @Select("select cr.*, c.* from contestregistry cr, contest c where STNumberId = #{STNumberId} and cr.cid = c.contestId order by cr._id desc;")
    @Results(value = {
            @Result(id = true, column = "_id", property = "_id"),
            @Result(column = "cid", property = "contest.contestId"),
            @Result(column = "STNumberId", property = "user._id"),
            @Result(column = "ClassAndGrade", property = "ClassAndGrade"),
            @Result(column = "email", property = "email"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "relName", property = "relName"),
            @Result(column = "contestId", property = "contest.contestId"),
            @Result(column = "contestName", property = "contest.contestName"),
            @Result(column = "contestIntroduction", property = "contest.contestIntroduction"),
            @Result(column = "contestTime", property = "contest.contestTime"),
            @Result(column = "contestNote", property = "contest.contestNote")
    })
    public List<ContestRegistry> findBySTNumberId(int STNumberId);

}
